﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class CardType : BaseAdmin
    {
        readonly DALProfessionAreaType theProfessionAreaType = new DALProfessionAreaType();
        readonly DALCardType theCardType = new DALCardType();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(ERule.CardType);
                if (Request.QueryString["action"] == "del")
                {
                    this.DeleteProfessionArea();
                }
                this.BindProfessionAreaList();
                this.BindGridView();
            }
        }

        void DeleteProfessionArea()
        {
            if (!theCardType.IsDelete(Request.QueryString["id"]))
            {
                Response.Write("<script> alert('卡片类别已被使用,无法删除!')</script>");
                return;
            }
            else
            {
                theCardType.Delete(int.Parse(Request.QueryString["id"]));
                Response.Write("<script> alert('删除成功!')</script>");
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.BindGridView();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.BindGridView();
        }


        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGridView();
            GridView1.Rows[e.NewEditIndex].FindControl("lbtnEdit").Visible = false;
            GridView1.Rows[e.NewEditIndex].FindControl("lbtnUpdate").Visible = true;
            GridView1.Rows[e.NewEditIndex].FindControl("lbtnCancel").Visible = true;
            ViewState["CardTypeName"] = ((TextBox)GridView1.Rows[e.NewEditIndex].Cells[0].Controls[0]).Text.Trim();

          // 
        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindGridView();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            double iOnSale = 1.0;
            try{
                iOnSale = double.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
                if(iOnSale <0.1 || iOnSale >= 1.0)
                {
                     base.Response.Write("<script language='javascript'>alert('折扣只能为0.1-1.0之间数字!');</script>");
                    return;
                }
            }catch
            {
                 base.Response.Write("<script language='javascript'>alert('折扣只能为0.1-1.0之间数字!');</script>");
                    return;
            }

            string sCardTypeName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();

            if (ViewState["CardTypeName"] != null && ViewState["CardTypeName"].ToString() != sCardTypeName)
            {
                tbCardType tempCardType = theCardType.GetModelByName(sCardTypeName);
                if (tempCardType != null)
                {
                    base.Response.Write("<script language='javascript'>alert('该礼品卡类别已存在,请重新输入礼品卡类别!');</script>");
                    return;
                }
            }
            tbCardType oCardType = theCardType.GetModel(int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString()));
            oCardType.Name = sCardTypeName;
            oCardType.ProfessionAreaTypeId = int.Parse(this.ddlProfessionArea.SelectedItem.Value);


            oCardType.OnSale = decimal.Parse(iOnSale.ToString());
            theCardType.Update(oCardType);

            Response.Write("<script> alert('修改成功!')</script>");
            GridView1.EditIndex = -1;
            this.BindGridView();
            
        }




        protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (this.txtCardType.Text.Trim() == "")
            {
               
                return;
            }
            tbCardType tempCardType = theCardType.GetModelByName(this.txtCardType.Text.Trim());
            if (tempCardType != null)
            {
                base.Response.Write("<script language='javascript'>alert('该礼品卡类别已存在,请重新输入礼品卡类别!');</script>");
                return;
            }
            tbCardType oCardType = new tbCardType();
            oCardType.Name = this.txtCardType.Text.Trim();
            oCardType.ProfessionAreaTypeId = int.Parse(this.ddlProfessionArea.SelectedItem.Value);
            theCardType.Add(oCardType);
           
            Response.Write("<script> alert('添加成功!')</script>");
            this.txtCardType.Text = "";
            this.BindGridView();
        }
        void BindProfessionAreaList()
        {
            DataSet ds;
            ds = theProfessionAreaType.GetList();
            if (ds != null && ds.Tables.Count > 0)
            {
                this.ddlProfessionArea.DataTextField = "Name";
                this.ddlProfessionArea.DataValueField = "Id";
                this.ddlProfessionArea.DataSource = ds.Tables[0].DefaultView;
                this.ddlProfessionArea.DataBind();
            }
        }

        void BindGridView()
        {
            DataSet ds;
            ds =  theCardType.GetList(int.Parse(this.ddlProfessionArea.SelectedItem.Value));
            DataTable table = ds.Tables[0];
            this.GridView1.DataSource = table;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void ddlProfessionArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindGridView();
        }
    }
}